﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Cache;
using System.Web.UI.WebControls;

/// <summary>
///  根据管理员权限，生成查询条件：分订单，商家，配送员
///  注意：有城市和区域。
/// </summary>
public class RightsTool
{
    public EAdminInfo admin = null;

    /// <summary>
    /// 获取当前登录的管理员
    /// </summary>
    public RightsTool()
    {
        admin = UserHelp.GetAdmin();
        if (admin == null)
        {
            HttpContext.Current.Response.Redirect("~/Admin/login.aspx?from=RightsTool");
        }
    }

    /// <summary>
    /// 传入登录的管理员
    /// </summary>
    public RightsTool(EAdminInfo _admin)
    {
        admin = _admin;
    }


    /// <summary>
    /// 订单查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getOrderWhere(string oldwhere)
    {
        return getOrderWhere(oldwhere, true);
    }

    /// <summary>
    /// 订单查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getOrderWhere(string oldwhere, bool issend)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }

        switch (admin.Permission)
        {
            case "10":
                sqlwhere += " and OldStatus=" + admin.ID + "";
                break;
            default:

                if (admin.CityID > 0)
                {
                    sqlwhere += " and Custorder.cityid=" + admin.CityID;
                }
                if (admin.AdminStatus > 0)
                {
                    if (issend == false)
                    {
                        sqlwhere += " and deliverheaderid=" + admin.AdminStatus;
                    }
                    else
                    {
                        //此处要修改成查看此区域下的配送员的订单
                        sqlwhere += " and EXISTS (SELECT dataid FROM dbo.Deliver WHERE GpsIMEI = " + admin.AdminStatus + " AND DataId = dbo.Custorder.deliverid)";
                    }

                }


                break;
        }


        return sqlwhere;
    }

    /// <summary>
    /// 订单查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getOrderWhere(string oldwhere, int groupid)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }

        switch (admin.Permission)
        {
            case "10":
                sqlwhere += " and OldStatus=" + admin.ID + "";
                break;
            default:

                if (admin.CityID > 0)
                {
                    sqlwhere += " and Custorder.cityid=" + admin.CityID;
                }

                sqlwhere += " and deliverheaderid=" + groupid;

                break;
        }


        return sqlwhere;
    }



    /// <summary>
    /// 商家查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getShopWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }

        switch (admin.Permission)
        {
            case "10":
                sqlwhere += " and PEnd='" + admin.ID + "'";
                break;
            default:

                if (admin.CityID > 0)
                {
                    sqlwhere += " and cityid=" + admin.CityID;
                }
                if (admin.AdminStatus > 0)
                {
                    sqlwhere += " and IsCallCenter=" + admin.AdminStatus;
                }

                break;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 订单查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getDeliverWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and Deliver.Inve1=" + admin.CityID;
        }
        if (admin.AdminStatus > 0)
        {
            sqlwhere += " and GpsIMEI=" + admin.AdminStatus;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 订单查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getDeliverWhere(string oldwhere, int groupid)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and Deliver.Inve1=" + admin.CityID;
        }

        sqlwhere += " and GpsIMEI=" + groupid;


        return sqlwhere;
    }


    /// <summary>
    /// 费用记录
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getAdminFeeWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and PayState=" + admin.CityID;
        }
        if (admin.AdminStatus > 0)
        {
            sqlwhere += " and PayType=" + admin.AdminStatus;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 管理员查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getAdminWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and CityID=" + admin.CityID;
        }
        if (admin.AdminStatus > 0)
        {
            sqlwhere += " and AdminStatus=" + admin.AdminStatus;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 群主查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getGroupWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and Status=" + admin.CityID;
        }
        if (admin.AdminStatus > 0)
        {
            sqlwhere += " and ID=" + admin.AdminStatus;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 物料主查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getDevicesWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and cityid=" + admin.CityID;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 物料主查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getDdeliverlogWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and reveint1=" + admin.CityID;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 考勤记录查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getDeliverWorkLogWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }


        if (admin.CityID > 0)
        {
            sqlwhere += " and EXISTS (SELECT dataid FROM dbo.Deliver WHERE Inve1 = " + admin.CityID + " AND dataid = DeliverWorkLog.deliverid)";


        }
        if (admin.AdminStatus > 0)
        {

            //此处要修改成查看此区域下的配送员的订单
            sqlwhere += " and EXISTS (SELECT dataid FROM dbo.Deliver WHERE GpsIMEI = " + admin.AdminStatus + " AND dataid = dbo.DeliverWorkLog.deliverid)";


        }
        return sqlwhere;
    }

    /// <summary>
    /// 商家备注查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getShopRemarkWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and Status=" + admin.CityID;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 城市查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getCityWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and cid=" + admin.CityID;
        }

        return sqlwhere;
    }


    /// <summary>
    /// 订单状态操作日志
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getOrderStateWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }


        if (admin.CityID > 0)
        {
            sqlwhere += " and EXISTS (SELECT unid FROM dbo.Custorder WHERE CityID = " + admin.CityID + " AND orderid = Record.orderid)";
        }
        return sqlwhere;
    }

    /// <summary>
    /// 订单操作日志
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getOrderOperateWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }


        if (admin.CityID > 0)
        {
            sqlwhere += " and EXISTS (SELECT unid FROM dbo.Custorder WHERE CityID = " + admin.CityID + " AND orderid = deliverlog.revevar1 )";
        }
        return sqlwhere;
    }

    /// <summary>
    /// 会员查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getUserWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and WebSite='" + admin.CityID + "'";
        }
        return sqlwhere;
    }


    /// <summary>
    /// 角色查询条件
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getRoleWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and cityid=" + admin.CityID;
        }

        return sqlwhere;
    }

    /// <summary>
    /// 微信ppt
    /// </summary>
    /// <param name="oldwhere"></param>
    /// <returns></returns>
    public string getPPTWhere(string oldwhere)
    {
        string sqlwhere = oldwhere;
        if (sqlwhere.Length == 0)
        {
            sqlwhere = " 1=1 ";
        }
        if (admin.CityID > 0)
        {
            sqlwhere += " and SecID=" + admin.CityID + "";
        }
        return sqlwhere;
    }

}
